package com.will.txj.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: wen-yi;
 * @date: 2021/12/4 17:04;
 * @Description:
 */
public class DesUtil {
    /**
     * @Description: 对字符串进行MD5加密
     * @Param:
     * @return:
     * @Author: guohailong
     * @Date: 2018/7/2
     */
    public static String getMD5Hash(String source){
        StringBuilder sb = new StringBuilder();
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] result = md5.digest(source.getBytes());
            for (byte b : result) {
                sb.append(String.format("%02X", b)); // 10进制转16进制，X 表示以十六进制形式输出，02 表示不足两位前面补0输出
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String EncryptString(String strInput, String txtKey) throws Exception {
        Cipher enCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 得到加密对象Cipher
        enCipher.init(Cipher.ENCRYPT_MODE, getKey(txtKey));// 设置工作模式为加密模式，给出密钥和向量
        byte[] pasByte = enCipher.doFinal(strInput.getBytes("utf-8"));
        return new String(Base64.encodeBase64(pasByte));
    }

    /**
     * 通过keyId解密对应的字符串
     * @param strInput
     * @param keyId
     * @return
     * @throws Exception
     */
    public static String DecryptString(String strInput, String keyId) throws Exception {
        Cipher deCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, getKey(keyId));
        byte[] pasByte = deCipher.doFinal(Base64.decodeBase64(strInput));
        return new String(pasByte, "UTF-8");
    }

    public static Key getKey(String txtKey) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(txtKey.getBytes());// 设置密钥参数
        //iv = new IvParameterSpec(DESIV);// 设置向量
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
        return keyFactory.generateSecret(keySpec);// 得到密钥对象
    }


    public static void main(String[] args) throws Exception {
//        String desKey = "d084fda59e8740ce998a93d42d34e335";
//        String iToken = "15928007618";
//        Long iKey = System.currentTimeMillis();
//        String iGroupId = "2";
//        String iGroupName = "成都";
//        String iDistrictId = "A10001";
//        String iString = "token="+iToken+"&key="+iKey+"&group_id="+iGroupId+"&group_name="+iGroupName+"&district_id="+iDistrictId;
//        System.out.println(iString);
//        String iEncrypt = EncryptString(iString, desKey);
//        System.out.println(iEncrypt);
//        String iDecrypt = DecryptString(iEncrypt, desKey);
//        System.out.println(iDecrypt);


        String admin = "admin";
        String key = "d084fda59e8740ce998a93d42d34e335";
        System.out.println("原文："+admin);
        System.out.println("密钥："+key);
        String s = EncryptString(admin, key);
        System.out.println("加密："+s);
        System.out.println("解密："+DecryptString(s,key));
        // gaVu4yPneds=
    }
}
